import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import IndexMaster from '@/views/mall/index.vue'
import User from '@/views/mall/user/index.vue'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/user/login/index'),
    hidden: true
  },

  {
    path: '/register',
    component: () => import('@/views/user/register/index.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/mall',
    component: IndexMaster,
    redirect: '/mall/index',
    hidden: true,
    children: [
      {
        path: 'index', // 添加斜杠
        name: 'Index',
        meta: { title: '首页' },
        component: () => import('@/views/mall/malls/index.vue')
      },
      {
        path: 'buy', // 添加斜杠
        name: 'buy',
        meta: { title: '商品详情' },
        component: () => import('@/views/mall/malls/BuysProduct.vue')
      },
      {
        path: 'myCart', // 添加斜杠
        name: 'myCart',
        meta: { title: '购物车' },
        component: () => import('@/views/mall/malls/Cart.vue')
      },
      {
        path: 'checkout', // 添加斜杠
        name: 'checkout',
        meta: { title: '购物车' },
        component: () => import('@/views/mall/malls/checkout.vue')
      },
      {
        path: 'user', // 添加斜杠
        name: 'user',
        meta: { title: '个人中心' },
        component: User,
        children: [
          {
            path: 'order', // 添加斜杠
            name: 'order',
            meta: { title: '我的订单' },
            component: () => import('@/views/mall/user/orderList.vue')
          },
          {
            path: 'userCenter', // 添加斜杠
            name: 'userCenter',
            meta: { title: '我的订单' },
            component: () => import('@/views/mall/user/userCenter.vue')
          }
        ]
      }

    ]
  },

  {
    path: '/',
    component: IndexMaster,
    redirect: '/mall/index'
  },

  {
    path: '/manager',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '工作台', icon: 'dashboard' }
    }]
  },

  {
    path: '/product',
    component: Layout,
    redirect: '/example/product',
    // name: 'Example',
    // meta: { title: '商品管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'table',
        component: () => import('@/views/shopback/product/index'),
        meta: { title: '商品管理', icon: 'table' }
      }
    ]
  },

  {
    path: '/order',
    component: Layout,
    redirect: '/example/order',
    // name: 'Example',
    // meta: { title: '商品管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'orderList',
        name: 'orderList',
        component: () => import('@/views/shopback/order/index'),
        meta: { title: '订单管理', icon: 'form' }
      }
    ]
  },

  {
    path: '/review',
    component: Layout,
    redirect: '/example/order',
    // name: 'Example',
    // meta: { title: '商品管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'table',
        component: () => import('@/views/shopback/order/index'),
        meta: { title: '商品评论', icon: 'form' }
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '使用文档', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
